package cn.edu.nuc.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.nuc.film.dao.OrderMapper;
import cn.edu.nuc.film.dao.RemainMapper;
import cn.edu.nuc.film.entity.Order;
import cn.edu.nuc.film.entity.Remain;
import cn.edu.nuc.film.service.interfaces.OrderService;
	
@Service
public class OrderServiceImpl implements OrderService {
	
	
	
	@Autowired
	private OrderMapper orderMapper;
	
	
	@Autowired
	private RemainMapper remainMapper;
	
	
	/**
	 * 获取用户的订单表
	 */
	@Override
	public List<Order> list(int userid) {
		List<Order> list = orderMapper.list(userid);
		return list;
	}

	
	
	/**
	 * 生成订单
	 */
	@Transactional
	public int creatOrder(Order order) {
		
		//获取要购买film的remain表里的余票信息
		Remain remain = remainMapper.remain(order.getGid());
		
		
		//首先对film做判断订单里 是否 有无该 film，下面也应进行进一步判断
		Order sameOrder = orderMapper.searchSameOrder(order.getUserid(), order.getFilmId(),order.getScreensid());
		/*System.out.println(sameOrder);*/
		try{
			//如果订单里可以查到该film，则进行下面的 购物数量求和操作
			if( sameOrder != null){
				
				/*判断输入的购物数量加上已经add到订单的物品的数量的和  与  该物品的库存比较
				若大于库存，则shop失败
				若小于或等于，则可以shop成功*/
				if( ( order.getOrdernum() + sameOrder.getOrdernum()) < remain.getFilmRemain()){
					
					/**
					 * 订单操作
					 */
					//若满足条件，则将购物车的数量  set为  当前的数量 加上 order里已有的数量之和
					sameOrder.setOrdernum( order.getOrdernum() + sameOrder.getOrdernum());
					int count1 = orderMapper.updateByPrimaryKeySelective(sameOrder);
					
					/**
					 * remain数量操作
					 */
					//更新remain表里的filmRemain
					int remainFilm = remain.getFilmRemain() - order.getOrdernum();
					remain.setFilmRemain(remainFilm);
					int count2 = remainMapper.updateByPrimaryKeySelective(remain);
					if( count1 > 0 && count2 > 0){
						return count1;
					}
					
				}
				
			}else{
				
				//更新remain表里的filmRemain
				int remainFilm1 = remain.getFilmRemain() - order.getOrdernum();
				remain.setFilmRemain(remainFilm1);
				//更新film表里的余票
				int count2 = remainMapper.updateByPrimaryKeySelective(remain);
				//更新order里film对应的购买票数
				int count1 = orderMapper.insert(order);
				if( count1 > 0 && count2 > 0){
					return count1;
				}
				
			}
		}catch(Exception e){
			throw new RuntimeException("购买失败！");
		}
		
		return 1;
	}
	
	/**
	 * refund 退票
	 */
	@Override
	@Transactional
	public int refund(int orderid) {
		
		//执行order表
		Order order = orderMapper.selectByPrimaryKey(orderid);
		int count1 = orderMapper.deleteByPrimaryKey(orderid);
		
		//只有退票成功后才执行film更新操作
		if( count1 > 0){
			
			//获取remain信息
			Remain remain = remainMapper.remain(order.getGid());
			System.out.println("remain------"+remain);
			
			//退票成功后的 film表里的余票(将refund的票添加到film表中)
			int filmRemain = remain.getFilmRemain() + order.getOrdernum();
			//对remain进行setFilmReamin操作
			remain.setFilmRemain(filmRemain);
			//更新remain里的余票(将refund的票添加到film表中)
			int updateRemain = remainMapper.updateByPrimaryKeySelective(remain);
			
			if( updateRemain > 0){
				return 1;
			}else{
				throw new RuntimeException("remain更新失败，退票失败！");
			}
		}else{
			throw new RuntimeException("退票失败！");
		}
		
	}

	
}
